package yxy.game.pm2.module.mail;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PickedTroopsSlot;
import yxy.game.pm2.bean.Soul;
import yxy.game.pm2.bean.currency.CurrencyGroup;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.message.MailMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.stat.StatHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

public class MailHandle {
    public void sendMail(String playerCode, MailTypeEnum type, String title, String content, CCReason reason, Collection<Currency> rewards) {
        MailBean mail = new MailBean();
        mail.setType(type);
        mail.setTitle(title);
        mail.setContent(content);
        if (rewards != null) {
            mail.setReason(reason);
            mail.getRewards().addAll(rewards);
            StatHandle.stat_资源增加(playerCode, reason, rewards);
        }
        DateTime now = DateTime.now();
        mail.setSendTime(now);
        mail.setExpiryTime(now.plusDays(7));

        try {
            MailBean.create(playerCode, mail);
        } catch (RedisException e) {
        }
    }

    public void sendMailToSystem(int areaId, MailTypeEnum type, String title, String content, CCReason reason, Collection<Currency> rewards) {
        MailBean mail = new MailBean();
        mail.setType(type);
        mail.setTitle(title);
        mail.setContent(content);
        if (rewards != null) {
            mail.setReason(reason);
            mail.getRewards().addAll(rewards);
        }
        DateTime now = DateTime.now();
        mail.setSendTime(now);
        mail.setExpiryTime(now.plusDays(7));

        try {
            MailBean.createToSystem(areaId, mail);
        } catch (RedisException e) {
        }
    }

    public Collection<MailMessage.MailInfo> packMails(String playerCode) throws RedisException {
        ArrayList<String> expired = new ArrayList<>();
        ArrayList<MailMessage.MailInfo> list = new ArrayList<>();
        Map<String, MailBean> all = MailBean.all(playerCode);
        for (MailBean mail : all.values()) {
            if (mail.expired()) {
                expired.add(mail.getCode());
                continue;
            }
            MailMessage.MailInfo.Builder builder = MailMessage.MailInfo.newBuilder();
            builder.setCode(mail.getCode());
            builder.setType(MailMessage.MailType.forNumber(mail.getType().getNumber()));
            builder.setTitle(mail.getTitle());
            builder.setSendtime(mail.getSendTime().getMillis());
            builder.setReaded(mail.isReaded());
            builder.setRewarded(mail.isRewarded() || mail.getRewards().size() <= 0);

            list.add(builder.build());
        }

        // 删除过期的邮件
        MailBean.remove(playerCode, expired);

        return list;
    }

    public MailMessage.MailInfo packMail(String playerCode, String code) {
        MailBean mailBean = MailBean.get(playerCode, code);
        if (mailBean == null) {
            return null;
        }
        return packMail(playerCode, mailBean);
    }

    public MailMessage.MailInfo packMail(String playerCode, MailBean mail) {
        MailMessage.MailInfo.Builder builder = MailMessage.MailInfo.newBuilder();
        builder.setCode(mail.getCode());
        builder.setType(MailMessage.MailType.forNumber(mail.getType().getNumber()));
        builder.setTitle(mail.getTitle());
        builder.setContent(mail.getContent());
        builder.setSendtime(mail.getSendTime().getMillis());
        builder.setExpirytime(mail.getExpiryTime().getMillis());
        builder.setReaded(mail.isReaded());
        builder.setRewarded(mail.isRewarded() || mail.getRewards().size() <= 0);
        builder.addAllRewards(new CurrencyHandle().packItems(mail.getRewards()));

        return builder.build();
    }

    public Collection<Currency> rewardMails(String playerCode, MailTypeEnum type) throws RedisException {
        ArrayList<Currency> rewardList = new ArrayList<>();
        Map<String, MailBean> all = MailBean.all(playerCode);
        int soulSize = Soul.all(playerCode).size();//魂玉数量
        int pickedTroopsSlotSize = PickedTroopsSlot.all(playerCode).size();//兵符数量
        for (MailBean mail : all.values()) {
            if (!mail.getType().equals(type) || mail.expired() || mail.isRewarded() || mail.getRewards().size() <= 0) {
                continue;
            }

            //todo 判断类型是魂玉或兵符，需要再去判断是否到达上限，上限则提示并continue
            Boolean isGone = true;
            ArrayList<Currency> rewards = mail.getRewards();
            for (Currency reward : rewards) {
                if ((reward.getType().getGroup().equals(CurrencyGroup.PickedTroopsSlot) && pickedTroopsSlotSize >= PickedTroopsSlot.maxNum)) {
                    isGone = false;
                    break;
                }

                if ((reward.getType().getGroup().equals(CurrencyGroup.SOUL) && soulSize >= Soul.maxNum)) {
                    isGone = false;
                    break;
                }
            }

            if (!isGone) continue;

            rewardList.addAll(mail.getRewards());
            mail.setReaded(true);
            mail.setRewarded(true);
            MailBean.set(playerCode, mail);

            new CurrencyHandle().addCurrency(playerCode, mail.getReason(), mail.getRewards());
        }
        Collection<Currency> rewards = Currency.merge(rewardList);
        return rewards;
    }

    public void removeReaded(String playerCode, MailTypeEnum type) throws RedisException {
        ArrayList<String> removed = new ArrayList<>();
        Map<String, MailBean> all = MailBean.all(playerCode);
        for (MailBean mail : all.values()) {
            if (!mail.getType().equals(type)) {
                continue;
            }
            if (mail.expired()) {
                removed.add(mail.getCode());
                continue;
            }
            if (!mail.isRewarded() && mail.getRewards().size() > 0) {
                continue;
            }
            if (mail.isReaded() || mail.isRewarded()) {
                removed.add(mail.getCode());
            }
        }
        MailBean.remove(playerCode, removed);
    }
}
